/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __frproto_cfg_get_param.c                          */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/30 by guojq                                */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "frproto_l7default.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

extern int __frproto_cfg_get_param(char *p_token_data
                                     , u_short *p_ip_proto
                                     , int *p_i_l7_proto);

void case1(void)
{
    char *p_token_data = frstrdup("tcp");
    u_short ip_proto = 0;
    int i_l7_proto = 0;
    int i_ret = 0;
    
    i_ret = __frproto_cfg_get_param(p_token_data, &ip_proto, &i_l7_proto);
    
    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    
    frfree(p_token_data);
    
    return;
}

void case2(void)
{
    char *p_token_data = frstrdup("tcp:");
    u_short ip_proto = 0;
    int i_l7_proto = 0;
    int i_ret = 0;
    
    i_ret = __frproto_cfg_get_param(p_token_data, &ip_proto, &i_l7_proto);
    
    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    
    frfree(p_token_data);
    
    return;
}

void case3(void)
{
    char *p_token_data = frstrdup("tcp:1433");
    u_short ip_proto = 0;
    int i_l7_proto = 0;
    int i_ret = 0;
    
    i_ret = __frproto_cfg_get_param(p_token_data, &ip_proto, &i_l7_proto);
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_EQUAL(ip_proto,IPPROTO_TCP);
    CU_ASSERT_EQUAL(i_l7_proto,1433);

    frfree(p_token_data);
    return;
}

void case4(void)
{
    char *p_token_data = frstrdup("udp:1");
    u_short ip_proto = 0;
    int i_l7_proto = 0;
    int i_ret = 0;
    
    i_ret = __frproto_cfg_get_param(p_token_data, &ip_proto, &i_l7_proto);
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_EQUAL(ip_proto,IPPROTO_UDP);
    CU_ASSERT_EQUAL(i_l7_proto,1);
    
    frfree(p_token_data);
    return;

}

void case5(void)
{
    char *p_token_data = frstrdup("any:85");
    u_short ip_proto = 0;
    int i_l7_proto = 0;
    int i_ret = 0;
    
    i_ret = __frproto_cfg_get_param(p_token_data, &ip_proto, &i_l7_proto);
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_EQUAL(ip_proto,IPPROTO_TCP|IPPROTO_UDP);
    CU_ASSERT_EQUAL(i_l7_proto,85);
    
    frfree(p_token_data);
    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("__frproto_cfg_get_param 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);
   	CU_add_test(pSuite, "case4", case4);
   	CU_add_test(pSuite, "case5", case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}